//
//  RMineViewController.m
//  Riji
//
//  Created by liujianlong on 2022/8/17.
//

#import "RMineViewController.h"
#import "RMineHeader.h"
#import "RMineCell.h"
#import "RLoginViewController.h"
#import "RMoreViewController.h"
#import "RAboutViewController.h"
#import "RNotesViewController.h"
#import "RPhotoViewController.h"
#import "RPersonViewController.h"
@interface RMineViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic, strong) NSArray *data;
@property (nonatomic, strong) RMineHeader *header;
@end

@implementation RMineViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initUI];
    [self loadData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(actionForUpdate:) name:notifyNoteUpdate object:nil];

}
- (void)initUI
{
    self.data = @[@[@"日记图库",@"回收站"],@[@"分享应用",@"更多设置"],@[@"关于我们"]];
    self.header = [RMineHeader loadFromXIB];
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(actionForTap)];
    [self.header addGestureRecognizer:tap];
    
    [self.tableView registerNib:[RMineCell nib] forCellReuseIdentifier:[RMineCell ID]];
}

- (void)loadData{
    int num1 = 0;
    int num2 = 0;
    int num3 = 0;
    NSString *str  = [NSDate currentTimeStamp:@"yyyy-MM-dd hh:mm"];
    NSDateComponents*date = [NSDate dateOfStamp:[str intValue]];
    int day = (int)date.day;
    for (int i = (int)date.weekday; i >= 1; i--) {
        NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@ and %@=%@ and %@=%@ and %@=%@",bg_sqlKey(@"ID"),bg_sqlValue(APP.user.ID),bg_sqlKey(@"year"),bg_sqlValue(@(date.year)),bg_sqlKey(@"month"),bg_sqlValue(@(date.month)),bg_sqlKey(@"day"),bg_sqlValue(@(day)),bg_sqlKey(@"remove"),bg_sqlValue(@(NO))];
        if (i == (int)date.weekday) {
            num1 = (int)[RNoteModel bg_count:nil where:where];
            num2 += num1;
        }else{
            num2 += (int)[RNoteModel bg_count:nil where:where];
        }
        day -= 1;
    }
    
    
    NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"ID"),bg_sqlValue(APP.user.ID),bg_sqlKey(@"remove"),bg_sqlValue(@(NO))];
    num3 = (int)[RNoteModel bg_count:nil where:where];
    self.header.todayNum.text = [NSString stringWithFormat:@"%d",num1];
    self.header.weekNum.text = [NSString stringWithFormat:@"%d",num2];
    self.header.totalNum.text = [NSString stringWithFormat:@"%d",num3];
}
#pragma mark - 登录
- (void)actionForTap
{
    if (APP.user.ID == LS(@"未知用户")) {
        RLoginViewController *vc = [RLoginViewController new];
        vc.type = RLoginTypeLogin;
        [self.navigationController pushViewController:vc animated:YES];
    }else{
        [self.navigationController pushViewController:[RPersonViewController new] animated:YES];
    }
}
#pragma mark - 登录后更新
- (void)actionForUpdate:(NSNotification *)notifi{
    [self loadData];
    [self.tableView reloadData];
    if (APP.user == nil || APP.user.token == nil ||APP.user.token.length <= 0 ) {
        return;
    }
    [CCHttp get:api_userinfo para:nil header:nil success:^(id  _Nonnull response) {
        NSString *token = APP.user.token;
        RUserModel *user = [RUserModel mj_objectWithKeyValues:[response valueForKey:@"data"]];
        user.token = token;
        APP.user = user;
        [RUserModel bg_clear:nil];
        [APP.user bg_saveOrUpdate];
        [self loadData];
        [self.tableView reloadData];
        [[NSNotificationCenter defaultCenter] postNotificationName:notifyUserUpdate object:nil];
    } fail:^(NSError * _Nonnull error) {}];
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return  [self.data[section] count];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    RMineCell *cell = [tableView dequeueReusableCellWithIdentifier:[RMineCell ID]];
    NSArray *array = self.data[indexPath.section];
    NSString *str = array[indexPath.row];
    cell.type = RMineCellTypeNormal;
    cell.img.image = [UIImage imageNamed:str];
    cell.name.text = LS(str);
    cell.line.hidden = indexPath.row == [array count] - 1;
    return  cell;
}
#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return section < 3 ? 10 : 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return  section == 0 ? self.header.frame.size.height : 0;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section < 3) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
        view.backgroundColor = [UIColor colorWithHexString:@"#F7F7F7"];
        return view;
    }
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        self.header.user = APP.user;
        return  self.header;
    }
    return nil;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * str = self.data[indexPath.section][indexPath.row];
    if ([str isEqualToString:@"日记图库"]) {
        NSString* where = [NSString stringWithFormat:@"where %@=%@ and %@=%@",bg_sqlKey(@"ID"),bg_sqlValue(APP.user.ID),bg_sqlKey(@"remove"),bg_sqlValue(@(NO))];
      int count = (int)[RNoteModel bg_count:nil where:where];
        if (count <=0 ) {
            return [RLHud msg:LS(@"还没发现图片，先去写个日记吧")];
        }
        [self.navigationController pushViewController:[RPhotoViewController new] animated:YES];
    }else if([str isEqualToString:@"回收站"]){
        RNotesViewController *vc = [RNotesViewController new];
        vc.type = NotesTypeRecycle;
        [self.navigationController pushViewController:vc animated:YES];
    }else if([str isEqualToString:@"分享应用"]){
        [Tool share:@[@"记事本"] target:self success:^(BOOL success, NSString * _Nonnull result) {
            
        }];
    }else if ([str isEqualToString:@"更多设置"]){
        [self.navigationController pushViewController:[RMoreViewController new] animated:YES];
    }else if ([str isEqualToString:@"关于我们"]){
        [self.navigationController pushViewController:[RAboutViewController new] animated:YES];
        
    }
    
}
@end
